package com.sos.core
{
	import com.sos.utils.ClassUtils;
	
	public class School
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------		
		
		public static const GENERIC:School			= new School(1, "Generic",		0xFFFFFF);
		public static const DEFENCE:School			= new School(2, "Defense",		0xD0F5FF);
		public static const PLAYMAKING:School		= new School(3, "Playmaking",	0x88D788);
		public static const PUNK:School				= new School(4, "Punk",			0xFFC6AA);
		public static const ATTACK:School			= new School(5, "Attack",		0xC6AAFF);
		
		public static const ALL:School				= new School(0, "All",			0xFFFFFF);
		public static const MULTISCHOOL:School		= new School(99,"Multi School",	0xF5D524);
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<School>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<School>();
			_list.push(School.GENERIC);
			_list.push(School.DEFENCE);
			_list.push(School.PLAYMAKING);
			_list.push(School.PUNK);
			_list.push(School.ATTACK);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _color:uint;

		// TODO: add Effect class instance
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function School(id:uint, name:String, color:uint)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("PlayerCardSchool enum already created!");
			// Init
			_id    = id;
			_name  = name;
			_color = color;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------		
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get color():uint
		{
			return _color;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names(add_all:Boolean=false,add_generic:Boolean=true):Array
		{
			var list:Array  = new Array;
			// Add fake ALL items
			if (add_all) list.push(School.ALL.name);
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				if (!add_generic && _list[i] == School.GENERIC) continue;
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list(add_all:Boolean=false,add_generic:Boolean=true):Array
		{
			var list:Array = new Array;
			// Add fake ALL items
			if (add_all) list.push(School.ALL);
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				if (!add_generic && _list[i] == School.GENERIC) continue;
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a School by given id 
		 * @param id Id of the card type to find
		 * @return PlayerRole constant for the given id, null if no School
		 * for the given id has been found
		 */		
		public static function getById(id:uint):School
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("School for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a PlayerRole by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByName(name:String):School
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("School for name '" + name + "' unknown!");
		}

	}
	
}